var globalFuncHook_AfterDataLoaded = formatDictField;

function getSchemas(sender)
{
    if($("#schemapane").text()=="")
    {
        $("#schemapane").load("webpart/schemas.php?action=getSchemas&t="+$.now());
    }
    else
    {
        $("#schemapane").toggle(500);
    }
}

function getTables(sender)
{
    var id=$(sender).attr("id");
    var schema = $("#"+id).text();
    if($("#"+id+"_").text()=="")
    {
        $("#"+id+"_").load("webpart/schemas.php?action=getTables&schema="+schema+"&t="+$.now());
    }
    else
    {
        $("#"+id+"_").toggle(500);
    }
}

var lastSelectedTableId;    // 上次选中的table的id
var gschema; // 当前选中的数据库
var gtable;  // 当前选中的表
function descTable(schema,table,sender)
{
    var p={
        url: 'tblDef.php?schema='+schema+'&table='+table+'&t='+$.now(),
        dataType: 'json',                
        colModel : [                        
            {display: '字段', name : 'Field', width : 150, sortable : true, align: 'left'},

            {display: '别名', name : 'Alias', width : 100, sortable : true, align: 'left'},
            {display: '描述', name : 'Description', width : 150, sortable : true, align: 'left'},
            {display: '字典', name : 'Dictionary', width : 150, sortable : true, align: 'left'},


            {display: '类型', name : 'Type', width : 150, sortable : true, align: 'left'},
            {display: '空值', name : 'Null', width : 40, sortable : true, align: 'left'},
            {display: '键', name : 'Key', width : 150, sortable : true, align: 'left'},
            {display: '默认值', name : 'Default', width : 150, sortable : true, align: 'left'},
            {display: '其他', name : 'Extra', width : 150, sortable : true, align: 'left'}               
        ],               
        buttons : [      
            {name: '编辑扩展信息', /*bclass: 'delete',*/ onpress : doCommand},                        
            {name: '隐藏扩展列', /*bclass: 'hideExtCol',*/ onpress : doCommand},                      
            {name: '显示扩展列', /*bclass: 'hideExtCol',*/ onpress : doCommand},
            {separator: true}                
        ],               
        searchitems : [                        
            {display: 'First Name', name : 'id'},       
        ],                            
        usepager: false,                
        title: schema+" - "+table,                
        useRp: true,                
        rp: 10,                
        showTableToggleBtn: false,                
        resizable: false,                
        width: 900,                
        height: 670,                
        singleSelect: true        
    };
    if($("#tblDef").text()!="")
    {
        $("#tblDef").flexOptions(p);
        $("#tblDef").flexReload();  
        $(".ftitle").text(schema+" - "+table);
    }
    else
    {
        $("#tblDef").flexigrid(p); 
    }
	
    $("#"+lastSelectedTableId).removeClass("selectedTable");
    var id = sender.id;
    $("#"+id).addClass("selectedTable");
    lastSelectedTableId = id;
	
    gschema = schema;
    gtable = table;
	
//    setTimeout("formatDictField()",100);
    
}

function doCommand(com, grid) 
{
    if (com == '隐藏扩展列') 
    {
        $("#tblDef").flexToggleCol("1",false);
        $("#tblDef").flexToggleCol("2",false);
        $("#tblDef").flexToggleCol("3",false);
    }
    else if(com == "显示扩展列")
    {
        $("#tblDef").flexToggleCol("1",true);
        $("#tblDef").flexToggleCol("2",true);
        $("#tblDef").flexToggleCol("3",true);                 
    }
    else if (com == '编辑扩展信息') 
    {
        if(document.getElementById('extDetail').style.display=='block')return;

        // 清空表单
        $(':input')
        .not(':button,:submit,:reset,:hidden')
        .val('')
        .removeAttr('checked')
        .removeAttr('selected');
        $("input[type='text']").val('');

        $('.trSelected', grid).each(function() 
        {
            var id = $(this).attr('id');
            id = id.substring(id.lastIndexOf('row')+3);
            $("input[name='field']").val(globalData.rows[id].cell[0] ); // hidden field
            $("input[name='schema']").val(gschema);
            $("input[name='table']").val(gtable);
            $("input[name='alias']").val(globalData.rows[id].cell[1]); // alias
            $("textarea[name='description']").val(globalData.rows[id].cell[2]);
            if(globalData.rows[id].cell[3]!="")
            {
                eval("var dict="+globalData.rows[id].cell[3]);
                var idx=0;
                for(var p in dict)
                {
                    $("input[name='key"+idx+"']").val(p);
                    $("input[name='val"+idx+"']").val(dict[p]);
                    idx++;
                }
            }

            var extDiv = document.getElementById("extDetail");
            extDiv.style.left = $("tr.trSelected").offset().left+"px";
            extDiv.style.top = ($("tr.trSelected").offset().top+$("tr.trSelected").height())+"px";

            if(parseInt(extDiv.style.top)+parseInt(extDiv.style.height)> document.documentElement.clientHeight)
            {
                extDiv.style.top=(parseInt(extDiv.style.top)-parseInt(extDiv.style.height)-$(this).height())+"px";
            }
            $("#extDetail").slideToggle(500);
        });
    }
}

function formatDictField()
{
	$("td[abbr='Dictionary'] div").each(
        function (){
            var id=$(this).parent("td").parent("tr").attr("id");
            id = id.substring(id.lastIndexOf('row')+3);
            var cellData = globalData.rows[id].cell[3];
			
            if(cellData=="")return;
            eval("var dict="+cellData);
            var dictHtml = '';
            var idx=0;
            for(var p in dict)
            {
                dictHtml+=p+":"+dict[p]+"<br>";
                idx++;
            }
            
            $(this).html(dictHtml);
        });
		$("td[abbr='Dictionary'] div").each(
        function (){
			
			$(this).addClass("wraptext");
		});
		
			$("td[abbr='Description'] div").each(
        function (){
            $(this).addClass("wraptext");
        });
}

$("#cancelBtn").click(function()
{
    $("#extDetail").slideToggle(500);
});


$("#submitBtn").click(function()
{
    $.post('tblExtEdit.php',$("#extForm").serialize(),function(data)
    {
        //alert("发送的数据------"+$("#extForm").serialize()+"返回的数据------"+data);
        if(data=="ok")
        {
            $("#extDetail").slideToggle(500);// 浮层消失
            setTimeout(function(){$("#tblDef").flexReload();/*setTimeout("formatDictField()",500);*/},500); 

        }
    });
    return false;
});

$("a").click(function(){
    alert($(this).text());
    return false;
});


